package com.zhehekeji.opsys.api.service.validate;

import java.util.Date;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TVotePlayer;
import com.zhehekeji.opsys.api.entity.TVoteRegion;
import com.zhehekeji.opsys.api.service.validate.args.VotePlayerValidateArgs;

/**
 * @ClassName VotePlayerValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-3-16   下午3:41:10
 */
@Service
public class VotePlayerValidate extends ValidateService<ValidateRets<TVotePlayer>, VotePlayerValidateArgs>{

	private final static Logger logger = LoggerFactory.getLogger(VotePlayerValidate.class);
	
	@Override
	public ValidateRets<TVotePlayer> validate(VotePlayerValidateArgs validateArgs)
			throws Exception {
		ValidateRets<TVotePlayer> validateRets = new ValidateRets<TVotePlayer>();
		SuperResp resp = validateArgs.getResp();
		String playerCode = validateArgs.getPlayerCode();
		Date currentDate = validateArgs.getCurrentDate();
		if (StringUtils.isEmpty(playerCode)){
			logger.error("参赛选手编码非法！");
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("参赛选手编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		TVotePlayer tmpTVotePlayer = entityDao.get(TVotePlayer.class, "o.code=?1", new Object[]{playerCode});
		if (tmpTVotePlayer == null){
			logger.error("参赛选手编码非法！");
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("参赛选手编码非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Integer tmpStatus = tmpTVotePlayer.getStatus();
		if (tmpStatus == 0){
			logger.error("参赛选手已经停用！");
			resp.setResultCode(LuckyVoteResultCode.PLAYER_HAS_BEEN_STOPPED);
			resp.setResultDesc("参赛选手已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Date tmpStartTime;
		Date tmpEndTime;
		
		TVoteRegion tmpTVoteRegion = tmpTVotePlayer.getTvoteRegion();
		if (tmpTVoteRegion != null){
			tmpStatus = tmpTVoteRegion.getStatus();
			if (tmpStatus == 0){
				logger.error("区域已经停用！");
				resp.setResultCode(LuckyVoteResultCode.REGION_HAS_BEEN_STOPPED);
				resp.setResultDesc("区域已经停用！");
				validateRets.setSuccess(false);
				return validateRets;
			}
			tmpStartTime = tmpTVoteRegion.getStartTime();
			tmpEndTime = tmpTVoteRegion.getEndTime();
			if (tmpStartTime != null){
				if (currentDate.compareTo(tmpStartTime) <0){
					logger.error("区域投票还未开始！");
					resp.setResultCode(LuckyVoteResultCode.REGION_HAS_YET_TO_BEGIN);
					resp.setResultDesc("区域投票还未开始！");
					validateRets.setSuccess(false);
					return validateRets;
				}
			}
			if (tmpEndTime != null){
				if (currentDate.compareTo(tmpEndTime) >0){
					logger.error("区域投票活动已经结束！");
					resp.setResultCode(LuckyVoteResultCode.REGION_HAS_COME_TO_AN_END);
					resp.setResultDesc("区域投票活动已经结束！");
					validateRets.setSuccess(false);
					return validateRets;
				}
			}
		}
		tmpStartTime = tmpTVotePlayer.getStartTime();
		tmpEndTime = tmpTVotePlayer.getEndTime();
		if (tmpStartTime != null){
			if (currentDate.compareTo(tmpStartTime) <0){
				logger.error("选手投票还未开始！");
				resp.setResultCode(LuckyVoteResultCode.PLAYER_HAS_YET_TO_BEGIN);
				resp.setResultDesc("选手投票还未开始！");
				validateRets.setSuccess(false);
				return validateRets;
			}
		}
		if (tmpEndTime != null){
			if (currentDate.compareTo(tmpEndTime) >0){
				logger.error("选手投票已经结束！");
				resp.setResultCode(LuckyVoteResultCode.PLAYER_HAS_COME_TO_AN_END);
				resp.setResultDesc("选手投票已经结束！");
				validateRets.setSuccess(false);
				return validateRets;
			}
		}
		
		validateRets.setEntity(tmpTVotePlayer);
		return validateRets;
		
	}

}

